--
-- Author: shunguo.chen
-- Date: 2019-07-01 20:36:56
--
-- AvatarshopRender
--

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.AvatarShopNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui.Panel:getContentSize())
    self:setAnchorPoint(cc.p(0.5,0.5))

    self._nameInitColor = self._ui.name:getTextColor()

    display.uiAddClick(self._ui.Panel, function()
        if self._data then
            if self._data:isNew() then
                self._data:setIsNew(false)
                self:refreshRedPoint()
            end

            executeFunc(self._onClick, "item_select", self)
        end
    end)

    self._ui.icon_from:setTouchEnabled(true)
    display.uiAddClick(self._ui.icon_from, function()
        if self._data then
            executeFunc(self._onClick, "get_resource", self)
        end
    end)

    self:refresh()
end

-- data = AvatarShopItem
function ClassRef:refresh()
    if self._data then
        local data = self._data
        local costItem = data:getCostItem()
        self._ui.Image:loadTexture(data:getIcon())
        self._ui.num:setString(costItem:getAmount())
        self._ui.icon:loadTexture(costItem:getIcon())

        local resource = data:getResource()
        local isActive = data:isActive()

        self._ui.icon_from:setVisible(resource ~= nil and not isActive)
        self._ui.mask:setVisible(not isActive)
        self._ui.Panel_num:setVisible(isActive)
        self._ui.name:setVisible(not isActive)
        if not isActive then
            self._ui.name:setTextColor(display.COLOR_GRAY)
            self._ui.name:setString(L("text_12300003"))
        end

        self:refreshRedPoint()
    end
end

function ClassRef:setEquiped(b)
    self._ui.equipped:setVisible(b)
    if b then
        self._ui.Panel_num:setVisible(false)
        self._ui.name:setVisible(true)
        self._ui.name:setTextColor(self._nameInitColor)
        self._ui.name:setString(L("barber_shoptips9"))
    end
end

function ClassRef:refreshRedPoint()
    self._ui.dot:setVisible(self._data:isNew())
end

function ClassRef:setSelected(b)
    self._ui.selected:setVisible(b)
end
function ClassRef:isSelected()
    return self._ui.selected:isVisible()
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
